home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / mordor_2.000 / mordor_2 / src / files3.c < prev    next >
C/C++ Source or Header  |  1994-07-07  |  9KB  |  401 lines

  1. /*
  2.  * FILES3.C:
  3.  *
  4.  *    File I/O Routines.
  5.  *
  6.  *    Copyright (C) 1991, 1992, 1993 Brett J. Vickers
  7.  *
  8.  */
  9.  
  10. #include "mstruct.h"
  11. #include "mextern.h"
  12. #include <string.h>
  13. #include <ctype.h>
  14.  
  15. /************************************************************************/
  16. /*                write_obj_to_mem            */
  17. /************************************************************************/
  18.  
  19. /* Save an object to a block of memory.                    */
  20. /* This function recursively saves the items that are contained inside    */
  21. /* the object as well.  If perm_only != 0 then only permanent objects    */
  22. /* within the object are saved with it.  This function returns the    */
  23. /* number of bytes that were written.                    */
  24.  
  25. int write_obj_to_mem(buf, obj_ptr, perm_only)
  26. char    *buf;
  27. object     *obj_ptr;
  28. char     perm_only;
  29. {
  30.     int     n, cnt, cnt2=0, error=0;
  31.     char    *bufstart;
  32.     otag    *op;
  33.  
  34.     bufstart = buf;
  35.  
  36.     memcpy(buf, obj_ptr, sizeof(object));
  37.     buf += sizeof(object);
  38.     
  39.     cnt = count_obj(obj_ptr, perm_only);
  40.     memcpy(buf, &cnt, sizeof(int));
  41.     buf += sizeof(int);
  42.  
  43.     if(cnt > 0) {
  44.         op = obj_ptr->first_obj;
  45.         while(op) {
  46.             if(!perm_only || (perm_only && 
  47.                (F_ISSET(op->obj, OPERMT) || 
  48.                F_ISSET(op->obj, OPERM2)))) {
  49.                 if((n = write_obj_to_mem(buf, 
  50.                     op->obj, perm_only)) < 0)
  51.                     error = 1;
  52.                 else
  53.                     buf += n;
  54.                 cnt2++;
  55.             }
  56.             op = op->next_tag;
  57.         }
  58.     }
  59.  
  60.     if(cnt != cnt2 || error)
  61.         return(-1);
  62.     else
  63.         return(buf - bufstart);
  64.  
  65. }
  66.  
  67. /************************************************************************/
  68. /*                write_crt_to_mem            */
  69. /************************************************************************/
  70.  
  71. /* Save a creature to memory.  This function                 */
  72. /* also saves all the items the creature is holding.  If perm_only != 0 */
  73. /* then only those items which the creature is carrying that are    */
  74. /* permanent will be saved.                        */
  75.  
  76. int write_crt_to_mem(buf, crt_ptr, perm_only)
  77. char        *buf;
  78. creature     *crt_ptr;
  79. char         perm_only;
  80. {
  81.     int     n, cnt, cnt2=0, error=0;
  82.     char    *bufstart;
  83.     otag    *op;
  84.  
  85.     bufstart = buf;
  86.  
  87.     memcpy(buf, crt_ptr, sizeof(creature));
  88.     buf += sizeof(creature);
  89.  
  90.     cnt = count_inv(crt_ptr, perm_only);
  91.     memcpy(buf, &cnt, sizeof(int));
  92.     buf += sizeof(int);
  93.  
  94.     if(cnt > 0) {
  95.         op = crt_ptr->first_obj;
  96.         while(op && cnt2<cnt) {
  97.             if(!perm_only || (perm_only && 
  98.                (F_ISSET(op->obj, OPERMT) || 
  99.                F_ISSET(op->obj, OPERM2)))) {
  100.                 if((n = write_obj_to_mem(buf, op->obj,
  101.                     perm_only)) < 0)
  102.                     error = 1;
  103.                 else
  104.                     buf += n;
  105.                 cnt2++;
  106.             }
  107.             op = op->next_tag;
  108.         }
  109.     }
  110.  
  111.     if(cnt != cnt2 || error)
  112.         return(-1);
  113.     else
  114.         return(buf - bufstart);
  115. }
  116.  
  117. /************************************************************************/
  118. /*                read_obj_from_mem            */
  119. /************************************************************************/
  120.  
  121. /* Loads the object from memory, returns the number of bytes read,    */
  122. /* and also loads every object which it might contain.  Returns -1 if    */
  123. /* there was an error.                            */
  124.  
  125. int read_obj_from_mem(buf, obj_ptr)
  126. char    *buf;
  127. object     *obj_ptr;
  128. {
  129.     int         n, cnt, error=0;
  130.     char        *bufstart;
  131.     otag        *op;
  132.     otag        **prev;
  133.     object         *obj;
  134.  
  135.     bufstart = buf;
  136.  
  137.     memcpy(obj_ptr, buf, sizeof(object));
  138.     buf += sizeof(object);
  139.  
  140.     obj_ptr->first_obj = 0;
  141.     obj_ptr->parent_obj = 0;
  142.     obj_ptr->parent_rom = 0;
  143.     obj_ptr->parent_crt = 0;
  144.     if(obj_ptr->shotscur > obj_ptr->shotsmax)
  145.         obj_ptr->shotscur = obj_ptr->shotsmax;
  146.  
  147.     memcpy(&cnt, buf, sizeof(int));
  148.     buf += sizeof(int);
  149.  
  150.     prev = &obj_ptr->first_obj;
  151.     while(cnt > 0) {
  152.         cnt--;
  153.         op = (otag *)malloc(sizeof(otag));
  154.         if(op) {
  155.             obj = (object *)malloc(sizeof(object));
  156.             if(obj) {
  157.                 if((n = read_obj_from_mem(buf, obj)) < 0)
  158.                     error = 1;
  159.                 else
  160.                     buf += n;
  161.                 obj->parent_obj = obj_ptr;
  162.                 op->obj = obj;
  163.                 op->next_tag = 0;
  164.                 *prev = op;
  165.                 prev = &op->next_tag;
  166.             }
  167.             else
  168.                 merror("read_obj", FATAL);
  169.         }
  170.         else
  171.             merror("read_obj", FATAL);
  172.     }
  173.  
  174.     if(error)
  175.         return(-1);
  176.     else
  177.         return(buf - bufstart);
  178. }
  179.  
  180. /************************************************************************/
  181. /*                read_crt_from_mem            */
  182. /************************************************************************/
  183.  
  184. /* Loads a creature from memory & returns bytes read.  The creature is    */
  185. /* loaded at the mem location specified by the second parameter.  In    */
  186. /* addition, all the creature's objects have memory allocated for them    */
  187. /* and are loaded as well.  Returns -1 on fail.                */
  188.  
  189. int read_crt_from_mem(buf, crt_ptr)
  190. char        *buf;
  191. creature     *crt_ptr;
  192. {
  193.     int         n, cnt, error=0;
  194.     char        *bufstart;
  195.     otag        *op;
  196.     otag        **prev;
  197.     object         *obj;
  198.  
  199.     bufstart = buf;
  200.  
  201.     memcpy(crt_ptr, buf, sizeof(creature));
  202.     buf += sizeof(creature);
  203.  
  204.     crt_ptr->first_obj = 0;
  205.     crt_ptr->first_fol = 0;
  206.     crt_ptr->first_enm = 0;
  207.     crt_ptr->parent_rom = 0;
  208.     crt_ptr->following = 0;
  209.     for(n=0; n<20; n++)
  210.         crt_ptr->ready[n] = 0;
  211.     if(crt_ptr->mpcur > crt_ptr->mpmax)
  212.         crt_ptr->mpcur = crt_ptr->mpmax;
  213.     if(crt_ptr->hpcur > crt_ptr->hpmax)
  214.         crt_ptr->hpcur = crt_ptr->hpmax;
  215.  
  216.     memcpy(&cnt, buf, sizeof(int));
  217.     buf += sizeof(int);
  218.  
  219.     prev = &crt_ptr->first_obj;
  220.     while(cnt > 0) {
  221.         cnt--;
  222.         op = (otag *)malloc(sizeof(otag));
  223.         if(op) {
  224.             obj = (object *)malloc(sizeof(object));
  225.             if(obj) {
  226.                 if((n = read_obj_from_mem(buf, obj)) < 0)
  227.                     error = 1;
  228.                 else
  229.                     buf += n;
  230.                 obj->parent_crt = crt_ptr;
  231.                 op->obj = obj;
  232.                 op->next_tag = 0;
  233.                 *prev = op;
  234.                 prev = &op->next_tag;
  235.             }
  236.             else
  237.                 merror("read_crt", FATAL);
  238.         }
  239.         else
  240.             merror("read_crt", FATAL);
  241.     }
  242.  
  243.     if(error)
  244.         return(-1);
  245.     else
  246.         return(buf - bufstart);
  247. }
  248.  
  249. /************************************************************************/
  250. /*                load_crt_tlk                */
  251. /************************************************************************/
  252.  
  253. /* This function loads a creature's talk responses if they exist.    */
  254.  
  255. int load_crt_tlk(crt_ptr)
  256. creature    *crt_ptr;
  257. {
  258.     char    crt_name[80], path[256];
  259.     char    keystr[80], responsestr[1024];
  260.     int    i, len1, len2;
  261.     ttag    *tp, **prev;
  262.     FILE    *fp;
  263.  
  264.     if(!F_ISSET(crt_ptr, MTALKS) || crt_ptr->first_tlk)
  265.         return(0);
  266.  
  267.     strcpy(crt_name, crt_ptr->name);
  268.     for(i=0; crt_name[i]; i++)
  269.         if(crt_name[i] == ' ')
  270.             crt_name[i] = '_';
  271.  
  272.     sprintf(path, "%s/talk/%s-%d", MONPATH, crt_name, crt_ptr->level);
  273.     fp = fopen(path, "r");
  274.     if(!fp) return(0);
  275.  
  276.     i = 0;
  277.     prev = &crt_ptr->first_tlk;
  278.     while(!feof(fp)) {
  279.         fgets(keystr, 80, fp);
  280.         len1 = strlen(keystr);
  281.         if(!len1) break;
  282.         keystr[len1-1] = 0;
  283.         fgets(responsestr, 1024, fp);
  284.         len2 = strlen(responsestr);
  285.         if(!len2) break;
  286.         responsestr[len2-1] = 0;
  287.  
  288.         i++;
  289.  
  290.         tp = (ttag *)malloc(sizeof(ttag));
  291.         if(!tp)
  292.             merror("load_crt_tlk", FATAL);
  293.         tp->key = (char *)malloc(len1);
  294.         if(!tp->key)
  295.             merror("load_crt_tlk", FATAL);
  296.         tp->response = (char *)malloc(len2);
  297.         if(!tp->response)
  298.             merror("load_crt_tlk", FATAL);
  299.         tp->next_tag = 0;
  300.  
  301.         strcpy(tp->key, keystr);
  302.         talk_crt_act(keystr,tp);
  303.         strcpy(tp->response, responsestr);
  304.  
  305.         *prev = tp;
  306.         prev = &tp->next_tag;
  307.     }
  308.  
  309.     fclose(fp);
  310.     return(i);
  311. }
  312. /***********************************************************************/
  313. /*                          talk_crt_act                               */
  314. /***********************************************************************/
  315. /* the talk_crt_act function, is passed the  key word line from a     *
  316.  * monster talk file, and parses the key word, as well as any monster *
  317.  * monster action (i.e. cast a spell, attack, do a social command)    *
  318.  * The parsed information is then assigned to the fields of the       *
  319.  * monster talkstructure. */
  320.  
  321. int talk_crt_act (str, tlk)
  322. char     *str;
  323. ttag    *tlk;
  324. {
  325.  
  326.     int     index =0, num =0,i, n;
  327.     char    *word[4];
  328.  
  329.  
  330.     if (!str){
  331.         tlk->key = 0;
  332.         tlk->action = 0;
  333.         tlk->target = 0;
  334.         tlk->type = 0;
  335.         return (0);
  336.     }    
  337.  
  338.     
  339.     for (i=0;i<4;i++)
  340.         word[i] = 0;
  341.  
  342.     for (n=0;n<4;n++){
  343.  
  344.         i=0;
  345.         while(isalpha(str[i +index]) || isdigit(str[i +index]) || 
  346.             str[i +index] == '-')
  347.             i++;
  348.         word[n] = (char *)malloc(sizeof(char)*i + 1);
  349.         if(!word[n])
  350.             merror("talk_crt_act", FATAL);
  351.  
  352.         memcpy(word[n],&str[index],i);
  353.         word[n][i] = 0;
  354.  
  355.         while(isspace(str[index +i]))
  356.             i++;
  357.     
  358.         index += i;
  359.         num++;
  360.         if(str[index] == 0)
  361.             break;
  362.  
  363.     }
  364.  
  365.     tlk->key = word[0];
  366.  
  367.     if (num < 2){
  368.         tlk->action = 0;
  369.         tlk->target = 0;
  370.         tlk->type = 0;
  371.         return(0);
  372.     }
  373.  
  374.     if (!strcmp(word[1],"ATTACK")){
  375.         tlk->type = 1;
  376.         tlk->target = 0;
  377.         tlk->action = 0;
  378.     }
  379.     else if(!strcmp(word[1],"ACTION") && num > 2){
  380.         tlk->type = 2;
  381.         tlk->action = word[2];
  382.         tlk->target = word[3];
  383.     }
  384.     else if(!strcmp(word[1],"CAST") && num > 2){
  385.         tlk->type = 3;
  386.         tlk->action = word[2];
  387.         tlk->target = word[3];
  388.     }
  389.     else if(!strcmp(word[1],"GIVE")){
  390.         tlk->type = 4;
  391.         tlk->action = word[2];
  392.         tlk->target =  0;
  393.     }
  394.     else{
  395.         tlk->type = 0;
  396.         tlk->action = 0;
  397.         tlk->target = 0;
  398.     }
  399.     return(0);
  400. }
  401.